{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "proved-check",
   "metadata": {},
   "source": [
    "# Colombian Identity Codes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daily-cornell",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "transsexual-lesson",
   "metadata": {},
   "source": [
    "The function `clean_co_nit()` cleans a column containing Colombian identity code (NIT) strings, and standardizes them in a given format. The function `validate_co_nit()` validates either a single NIT strings, a column of NIT strings or a DataFrame of NIT strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dried-spyware",
   "metadata": {},
   "source": [
    "NIT strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"2131234321\"\n",
    "* `standard`: NIT strings with proper whitespace in the proper places, like \"213.123.432-1\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_co_nit()` and `validate_co_nit()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "saving-beijing",
   "metadata": {},
   "source": [
    "### An example dataset containing NIT strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "micro-eligibility",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"nit\": [\n",
    "            \"2131234321\",\n",
    "            \"2131234325\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "underlying-split",
   "metadata": {},
   "source": [
    "## 1. Default `clean_co_nit`\n",
    "\n",
    "By default, `clean_co_nit` will clean nit strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ruled-dream",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_co_nit\n",
    "clean_co_nit(df, column = \"nit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "medium-equity",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "funny-variety",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "filled-player",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fitted-humanity",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_co_nit(df, column = \"nit\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "swiss-boating",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vocational-target",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_co_nit(df, column = \"nit\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "encouraging-navigator",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NIT strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wireless-block",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_co_nit(df, column=\"nit\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "extraordinary-ranking",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "republican-alaska",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baking-grass",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_co_nit(df, \"nit\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lonely-rotation",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brave-memory",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_co_nit(df, \"nit\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regional-architecture",
   "metadata": {},
   "source": [
    "## 4. `validate_co_nit()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "digital-ghana",
   "metadata": {},
   "source": [
    "`validate_co_nit()` returns `True` when the input is a valid NIT. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_co_nit()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_co_nit()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_co_nit()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ignored-party",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_co_nit\n",
    "print(validate_co_nit(\"2131234321\"))\n",
    "print(validate_co_nit(\"2131234325\"))\n",
    "print(validate_co_nit(\"51824753556\"))\n",
    "print(validate_co_nit(\"51 824 753 556\"))\n",
    "print(validate_co_nit(\"hello\"))\n",
    "print(validate_co_nit(np.nan))\n",
    "print(validate_co_nit(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "returning-apollo",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "raising-angel",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_co_nit(df[\"nit\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "departmental-sunrise",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "frozen-killer",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_co_nit(df, column=\"nit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "correct-madrid",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "patient-legend",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_co_nit(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "molecular-elizabeth",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
